package com.xuliucar.car.view.dialog

import android.app.Activity
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.PopupWindow
import com.cangmaomao.lib.utils.SPUtil
import com.cangmaomao.lib.utils.TypeUtils
import com.cangmaomao.lib.utils.e
import com.dingmouren.colorpicker.ColorPickerDialog
import com.dingmouren.colorpicker.OnColorPickerListener
import com.xuliucar.car.R
import com.xuliucar.car.adapter.ColorSelectAdapter
import com.xuliucar.car.event.ColorEvent
import de.hdodenhof.circleimageview.CircleImageView
import org.greenrobot.eventbus.EventBus


@Suppress("DEPRECATION")
/**
 * Author:帅气的potato
 */
class ColorPop(val context: Context?, val sp: SPUtil) : PopupWindow(context) {

    private var mContext: Context = context!!
    private var adapter: ColorSelectAdapter = ColorSelectAdapter()
    private lateinit var setList: HashSet<String>
    private lateinit var layoutManger: LinearLayoutManager

    private var supportAlpha: Boolean = false
    private lateinit var mColorPickerDialog: ColorPickerDialog

    init {
        initPop()
    }


    fun initPop() {
        val popupView = LayoutInflater.from(mContext).inflate(R.layout.color_pop, null)
        contentView = popupView
        val footer = LayoutInflater.from(mContext).inflate(R.layout.item_color_footer, null)
        val iv_image = footer.findViewById<CircleImageView>(R.id.iv_add)
        val colorRecyclerView = popupView.findViewById<RecyclerView>(R.id.colorPopView)
        layoutManger = LinearLayoutManager(mContext)
        layoutManger.orientation = LinearLayoutManager.HORIZONTAL
        colorRecyclerView.layoutManager = layoutManger
        colorRecyclerView.adapter = adapter
        getColorList()
        adapter.addFootView(footer)
        setBackgroundDrawable(ColorDrawable())
        isFocusable = true
        isOutsideTouchable = false
        isTouchable = true
        val c = (mContext as Activity)
        setOnDismissListener {
            backgroundAlpha(c, 1.0f)
        }
        width = LinearLayout.LayoutParams.MATCH_PARENT
        height = LinearLayout.LayoutParams.WRAP_CONTENT
        setBackgroundDrawable(c.resources.getDrawable(android.R.color.transparent))
        setTouchInterceptor { _, _ -> false }
        var adapterListener: ColorSelectAdapter.colorCall = object : ColorSelectAdapter.colorCall {
            override fun selectColor(color: Int?) {
                EventBus.getDefault().post(ColorEvent(color!!))
                dismiss()
            }
        }
        adapter.setColorListener(adapterListener)
        iv_image.setOnClickListener {
            mColorPickerDialog = ColorPickerDialog(context, context!!.getResources().getColor(R.color.colorPrimary), supportAlpha, mOnColorPickerListener).show()
            supportAlpha = !supportAlpha
        }
    }


    private fun getColorList() {
        val s = sp.get("color", HashSet<String>()) as HashSet<String>
        setClassList(s)
    }


    private fun setClassList(list: HashSet<String>) {
        this.setList = HashSet(list)
        val colorList = ArrayList<Int>()
        for (i in setList) {
            colorList.add(TypeUtils.paseInt(i))
        }
        adapter.addList(colorList)
    }

    fun upColor(color: Int) {
        setList.add("$color")
        adapter.addData(color)
        sp.add("color", setList)
    }

    var colordata: colorCall? = null

    fun setColorData(colordata: colorCall) {
        this.colordata = colordata
    }

    interface colorCall {
        fun colorData(color: Int)
    }

    fun backgroundAlpha(c: Activity, bgalpha: Float) {
        val lp = c.window.attributes
        lp.alpha = bgalpha //0.0-1.0
        c.window.attributes = lp
    }

    private val mOnColorPickerListener = object : OnColorPickerListener {
        override fun onColorCancel(dialog: ColorPickerDialog) {

        }

        override fun onColorChange(dialog: ColorPickerDialog, color: Int) {
        }

        override fun onColorConfirm(dialog: ColorPickerDialog, color: Int) {
            upColor(color)
        }
    }

}